function [ll,BIC,invA0,Atau,Hlambda] = cicaar(X,L,M,astart)
% CICAAR     maximum likelihood Convolutive ICA with an Auto-Regressive 
%            inverse model. This is a wrapper that calls the binary CICAAR.
%
% synopsis:
%
%    [ll,BIC,invA0,Atau,Hlambda] = cicaar(X,L,M,astart)
%    [ ... ]                     = cicaar(X,L,M) 
%    [ ... ]                     = cicaar(X,L)
%    [ ... ]                     = cicaar(X)
%
% Inputs:
%
%        X : input data , D-by-T
%        L : Number of lagged mixing matrices.
%        M : Source color model order.
%   astart : Initialization method (see CICAAR documentation).
%
% Outputs:
%
%        ll : final value of log-likelihood.
%       BIC : Model posterior estimate.
%     invA0 : inverse of zero-lag mixing matrix, inv(D-by-K) = K-by-D
%      Atau : lagged mixing matrices, D-by-K-by-L
%   Hlambda : Source FIR models
% 
% Notes / see also:
%
%    To compute the sources use CICAARSEP, i.e.
%
%       S = cicaarsep(invA0,Atau,X);
%
%    To reconstruct a source (source #) in sensor domain use CICAARMIX, i.e.
%
%       Xk = cicaarmix(invA0,Atau,S,k); % k is the source number
%
% References:  
%
%   [1] Dyrholm, M., Makeig, S., Hansen, L. K., "Model selection for
%       convolutive ICA with an application to spatio-temporal Analysis
%       of EEG", Neural Computation, 19(4):934-955, 2007
%  
%   [2] Dyrholm, M., Makeig, S., Hansen, L. K., "Model structure selection
%       in convolutive mixtures", Independent Component Analysis and Blind
%       Signal Separation, Springer LNCS vol. 3889, pp. 74-81, 2006'
% 
%   [3] Dyrholm, M., Hansen, L. K., "CICAAR: Convolutive ICA with
%       an Auto-Regressive Inverse Model", Independent Component Analysis
%       and Blind Signal Separation, vol. 3195, pp. 594-601, 2004
%
% Copyright: Mads Dyrholm, 2007. 

%
% @Article{pmid17348768,
%   Author="Dyrholm, Mads and Makeig, Scott and Hansen, Lars Kai",
%   Title="{{M}odel selection for convolutive {I}{C}{A} with an application to spatiotemporal analysis of {E}{E}{G}}",
%   Journal="Neural Comput",
%   Year="2007",
%   Volume="19",
%   Number="4",
%   Pages="934--955",
%  Month="Apr"
% }
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Settings... 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rand('twister',sum(100*clock));
CICAAR_BINARY = '/Users/mads/professor/Columbia/code/cicaar/cicaar_fortran/cicaar'; % 
randnum  = round(rand*100000);
TMP_FILE1 = sprintf('~/X%i.mad',randnum);
TMP_FILE2 = sprintf('~/A%i.mad',randnum);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin<4,
 astart = 0;
end
if nargin<3
 M = 0;
end
if nargin<2
 L = 0;
end
cicaarwrite(TMP_FILE1,X);
D = size(X,1);
KC = D;
eval(sprintf('!%s -infile %s -outfile %s -KC%i -L%i -M%i -astart%f',CICAAR_BINARY,TMP_FILE1,TMP_FILE2,KC,L,M,astart));
[invA0,Atau,ll,diagE,intercept,interceptE,Hlambda,E] = cicaarread(TMP_FILE2);
delete(TMP_FILE1);
delete(TMP_FILE2);
BIC = cicaar_bayes(D,KC,L,M,~isempty(intercept),ll,size(X,2))    % BIC